package com.huawei.appgallery.foundation.ui.framework.cardframe.widget;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.huawei.appgallery.foundation.ui.framework.cardframe.bean.BaseDetailResponse.DataFilterSwitch;
import huawei.widget.HwSwitch;
import java.lang.ref.WeakReference;
import java.util.LinkedHashMap;
import o.azb;
import o.dgh;
import o.dgm;
import o.dhi;
import o.dhj;
import o.dhm;
import o.dky.h;
import o.dky.i;
import o.dky.j;
import o.Logger;
import o.doo;
import o.eua;
import o.evc;
import org.json.JSONException;
import org.json.JSONObject;

public class FilterDataLayout extends LinearLayout implements dhi, dhj {
    private dhm ʼ;
    private HwSwitch ˊ;
    private View ˋ;
    private Context ˎ;
    private TextView ˏ;
    private LayoutInflater ॱ;
    private DataFilterSwitch ᐝ;

    public static class b implements OnCheckedChangeListener {
        private WeakReference<FilterDataLayout> ˏ;

        public b(FilterDataLayout filterDataLayout) {
            this.ˏ = new WeakReference(filterDataLayout);
        }

        public void onCheckedChanged(CompoundButton compoundButton, boolean z) {
            boolean z2 = true;
            if (!evc.ˋ()) {
                boolean z3;
                if (z) {
                    z3 = false;
                } else {
                    z3 = true;
                }
                compoundButton.setChecked(z3);
            } else if (this.ˏ != null && this.ˏ.get() != null) {
                FilterDataLayout filterDataLayout = (FilterDataLayout) this.ˏ.get();
                if (filterDataLayout == null) {
                    Logger.e("FilterDataLayout", "filterDataLayout == null");
                    return;
                }
                Context context = filterDataLayout.getContext();
                if (!doo.ˊ(context)) {
                    if (context != null) {
                        Toast.makeText(context, context.getString(j.ॱʼ), 0).show();
                    }
                    if (z) {
                        z2 = false;
                    }
                    compoundButton.setChecked(z2);
                } else if (filterDataLayout.ˊ == null) {
                    Logger.e("FilterDataLayout", "filterSwitch == null");
                } else {
                    filterDataLayout.ˊ.setOnCheckedChangeListener(null);
                    compoundButton.setChecked(z);
                    filterDataLayout.ˊ.setOnCheckedChangeListener(this);
                    dhm ˊ = filterDataLayout.ʼ;
                    if (ˊ != null) {
                        DataFilterSwitch ˏ = filterDataLayout.ᐝ;
                        if (ˏ != null) {
                            ˏ.ˋ(z ? ˏ.ˊ() : ˏ.ॱ());
                            dgh.ˋ("datafilter_switchbtn_click", ˎ(filterDataLayout.ˎ, ˏ));
                        }
                        FilterDataLayout.ˏ(ˏ);
                        ˊ.ˋ(ˏ);
                    }
                }
            }
        }

        private LinkedHashMap<String, String> ˎ(Context context, DataFilterSwitch dataFilterSwitch) {
            Activity activity;
            LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap();
            linkedHashMap.put("switchval", dataFilterSwitch.ˎ());
            linkedHashMap.put("name", dataFilterSwitch.ˋ());
            linkedHashMap.put("para", dataFilterSwitch.ˏ());
            if (context instanceof Activity) {
                activity = (Activity) context;
            } else {
                context = null;
            }
            linkedHashMap.put("service_type", String.valueOf(dgm.ˎ(activity)));
            return linkedHashMap;
        }
    }

    public FilterDataLayout(Context context) {
        super(context);
        ˎ(context);
    }

    public FilterDataLayout(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        ˎ(context);
    }

    public FilterDataLayout(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        ˎ(context);
    }

    public static void ˋ(DataFilterSwitch dataFilterSwitch) {
        DataFilterSwitch ˊ = ˊ();
        if (ˊ != null && dataFilterSwitch != null) {
            dataFilterSwitch.ˋ(ˊ.ˎ());
            ˏ(dataFilterSwitch);
        }
    }

    public static String ˏ() {
        return eua.ˏ().ॱ(ॱ(), "");
    }

    public static DataFilterSwitch ˊ() {
        Object ˏ = ˏ();
        if (TextUtils.isEmpty(ˏ)) {
            return null;
        }
        try {
            JSONObject jSONObject = new JSONObject(ˏ);
            DataFilterSwitch dataFilterSwitch = new DataFilterSwitch();
            dataFilterSwitch.fromJson(jSONObject);
            if (dataFilterSwitch.ʽ()) {
                return dataFilterSwitch;
            }
            return null;
        } catch (JSONException e) {
            Logger.e("FilterDataLayout", "getCacheFilterSwitch, JSONException!");
            return null;
        } catch (InstantiationException e2) {
            Logger.e("FilterDataLayout", "getCacheFilterSwitch, InstantiationException!");
            return null;
        } catch (ClassNotFoundException e3) {
            Logger.e("FilterDataLayout", "getCacheFilterSwitch, ClassNotFoundException!");
            return null;
        } catch (IllegalAccessException e4) {
            Logger.e("FilterDataLayout", "getCacheFilterSwitch, ClassNotFoundException!");
            return null;
        }
    }

    private static String ॱ() {
        return "filterValue_key";
    }

    private void ˎ(Context context) {
        this.ˎ = context;
        if (this.ˋ == null) {
            this.ॱ = LayoutInflater.from(getContext());
            this.ˋ = this.ॱ.inflate(i.ॱˊ, null);
            azb.ˏ(this.ˋ);
            this.ˏ = (TextView) this.ˋ.findViewById(h.ॱʼ);
            this.ˊ = (HwSwitch) this.ˋ.findViewById(h.ـ);
            this.ˊ.setOnCheckedChangeListener(new b(this));
            addView(this.ˋ);
        }
    }

    public void setDataFilterListener(dhm o_dhm) {
        this.ʼ = o_dhm;
    }

    public void setFilterData(DataFilterSwitch dataFilterSwitch) {
        this.ᐝ = dataFilterSwitch;
        ˊ(dataFilterSwitch);
    }

    private void ˊ(DataFilterSwitch dataFilterSwitch) {
        if (this.ˏ == null || this.ˊ == null || dataFilterSwitch == null || !dataFilterSwitch.ʽ()) {
            setVisibility(8);
            return;
        }
        setVisibility(0);
        this.ˏ.setText(dataFilterSwitch.ˋ());
        this.ˊ.setOnCheckedChangeListener(null);
        this.ˊ.setChecked(dataFilterSwitch.ˊ().equals(dataFilterSwitch.ˎ()));
        this.ˊ.setOnCheckedChangeListener(new b(this));
    }

    public void setVisibility(int i) {
        if (getVisibility() != i) {
            super.setVisibility(i);
        }
    }

    private static void ˏ(DataFilterSwitch dataFilterSwitch) {
        if (dataFilterSwitch != null && dataFilterSwitch.ʽ() && !TextUtils.isEmpty(dataFilterSwitch.ˎ())) {
            try {
                eua.ˏ().ˋ(ॱ(), dataFilterSwitch.toJson());
            } catch (IllegalAccessException e) {
                Logger.e("FilterDataLayout", "saveFilterString, IllegalAccessException!");
            }
        }
    }
}
